#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int data;
} MyStruct;

MyStruct* my_realloc(MyStruct* old_ptr, size_t new_size, size_t old_size) 
{
    if (new_size == 0) 
    {
        free(old_ptr);
        return NULL;
    }

    MyStruct* new_ptr = (MyStruct*)malloc(new_size * sizeof(MyStruct));
    if (new_ptr == NULL) 
    {
        return NULL;
    }

    if (old_ptr != NULL) 
    {
        size_t copy_size = old_size < new_size ? old_size : new_size;
        memcpy(new_ptr, old_ptr, copy_size * sizeof(MyStruct));
        free(old_ptr);
    }

    return new_ptr;
}

int main(int argc, char const *argv[]) 
{
    MyStruct* arr = (MyStruct*)malloc(3 * sizeof(MyStruct));
    if (arr == NULL) 
    {
        printf("分配失败!\n");
        return 1;
    }

    arr[0].data = 1;
    arr[1].data = 2;
    arr[2].data = 3;

    MyStruct* new_arr = my_realloc(arr, 5, 3);
    if (new_arr == NULL) 
    {
        printf("分配失败!\n");
        return 1;
    }

    new_arr[3].data = 4;
    new_arr[4].data = 5;

    for (int i = 0; i < 5; i++) 
    {
        printf("%d ", new_arr[i].data);
    }
    printf("\n");

    free(new_arr);
    return 0;
}
